//
//  ZDHomeViewController.m
//  1012GiftTips
//
//  Created by Apple on 15/10/15.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDHomeViewController.h"
#import "ZDLabel.h"

#import "ZDJingXuanViewController.h"
#import "ZDGiftViewController.h"
#import "ZDFoodViewController.h"
#import "ZDDigitViewController.h"
#import "ZDSportViewController.h"
#import "ZDAmusementViewController.h"

#define titleNum 6
#define ZDLineHeight 3
#define ZDTopScrollHeight self.topScrollView.frame.size.height
#define ZDPadding 10

@interface ZDHomeViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *topScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *bottomScrollView;

@property (nonatomic, strong) NSMutableArray *centerArr;

@property (nonatomic, strong) NSMutableArray *widthArr;

@property (nonatomic, strong) UIView *line;

@end

@implementation ZDHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
#warning 不能在当前控制器中通过设置self.title设置标题，会直接影响到tabBar中item的标题，即会连带tabBar中的item标题一起设置成一样。
//    self.title = @"礼物说";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Feed_ScanIcon"] style:UIBarButtonItemStyleDone target:self action:@selector(scanAction)];
    
    //设置bottomScrollView
    [self setUpBottomScrollView];
    
    //指定代理
    self.bottomScrollView.delegate = self;
    
    //设置topScrollView
    [self setUpTopScrollView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}


#pragma mark - 左边扫描按钮回调方法
- (void)scanAction
{
    //请求相机
}


//设置topScrollView
- (void)setUpTopScrollView
{
    CGFloat labelW = 70;
    CGFloat labelH = ZDTopScrollHeight - ZDLineHeight;
    CGFloat labelY = 0;
    
    for (int index = 0; index < titleNum; index++)
    {
        ZDLabel *label = [[ZDLabel alloc] initWithFrame:CGRectMake(index * labelW, labelY, labelW, labelH)];
#warning initWithframe与直接frame设置label的属性有区别？？？
//        ZDLabel *label = [[ZDLabel alloc] init];
//        
//        label.frame = CGRectMake(index * labelW, labelY, labelW, labelH);
        
//        ZDLog(@"label: %@", NSStringFromCGRect(label.frame));
        
        label.text = [self.childViewControllers[index] title];
        
        if (index == 0)
        {
            label.scale = 1.0;
        }
        
        //打开label的用户交互
        label.userInteractionEnabled = YES;
        
        //创建点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
        //添加到label中
        [label addGestureRecognizer:tapGesture];
        
        [self.topScrollView addSubview:label];
        
        NSNumber *num1 = [NSNumber numberWithFloat:label.center.x];
        [self.centerArr addObject:num1];
        NSNumber *num2 = [NSNumber numberWithFloat:labelW-ZDPadding];
        [self.widthArr addObject:num2];
    }
    
    //设置contentSize
    self.topScrollView.contentSize = CGSizeMake(titleNum * labelW, 0);
    
    //去掉水平滚动条
    self.topScrollView.showsHorizontalScrollIndicator = NO;
    //去掉垂直滚动条
    self.topScrollView.showsVerticalScrollIndicator = NO;
    
    NSNumber *num1 = [self.centerArr firstObject];
    NSNumber *num2 = [self.widthArr firstObject];
    self.line.frame = CGRectMake(num1.floatValue - num2.floatValue * 0.5, ZDTopScrollHeight - ZDLineHeight, num2.floatValue, ZDLineHeight);
    [self.topScrollView addSubview:self.line];
//    ZDLog(@"%@",NSStringFromCGRect(self.line.frame));
}

//label的点击手势回调方法
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)gesture
{
    //    ZDLog(@"%@", gesture);
    
    ZDLabel *label = (ZDLabel *)gesture.view;
    
    NSInteger index = [self.topScrollView.subviews indexOfObject:label];
    
    //    ZDLog(@"%lf", self.bottomScrollView.frame.origin.y);     //注意不为0，而是30
    
    [self.bottomScrollView setContentOffset:CGPointMake(index * self.bottomScrollView.frame.size.width, 0) animated:YES];
    
    [self scrollLineToLabel:label];
}

//设置bottomScrollView
- (void)setUpBottomScrollView
{
    ZDJingXuanViewController *JingXuanVc = [[ZDJingXuanViewController alloc] init];
    JingXuanVc.title = @"精选";
    //添加到当前控制器中
    [self addChildViewController:JingXuanVc];
    
    ZDGiftViewController *giftVc = [[ZDGiftViewController alloc] init];
    giftVc.title = @"礼物";
    //添加到当前控制器中
    [self addChildViewController:giftVc];
    
    ZDFoodViewController *foodVc = [[ZDFoodViewController alloc] init];
    foodVc.title = @"美食";
    //添加到当前控制器中
    [self addChildViewController:foodVc];
    
    ZDDigitViewController *digitVc = [[ZDDigitViewController alloc] init];
    digitVc.title = @"数码";
    //添加到当前控制器中
    [self addChildViewController:digitVc];
    
    ZDSportViewController *sportVc = [[ZDSportViewController alloc] init];
    sportVc.title = @"运动";
    //添加到当前控制器中
    [self addChildViewController:sportVc];
    
    ZDAmusementViewController *amusementVc = [[ZDAmusementViewController alloc] init];
    amusementVc.title = @"娱乐";
    //添加到当前控制器中
    [self addChildViewController:amusementVc];
    
    
    CGFloat VCViewW = self.bottomScrollView.frame.size.width;    //ZDScreenSize.width
    CGFloat VCViewH = self.bottomScrollView.frame.size.height;
    //    CGFloat VCViewH = ZDScreenSize.height - 64 - self.topScrollView.frame.size.height;
    CGFloat VCViewY = 0;
    
    //一开始，默认添加第一个控制器的view到scrollView中，其他view在滚动时动态添加
    CGFloat VCViewX = 0;
    JingXuanVc.view.frame = CGRectMake(VCViewX, VCViewY, VCViewW, VCViewH);
    [self.bottomScrollView addSubview:JingXuanVc.view];
    
    //设置contentSize
    self.bottomScrollView.contentSize = CGSizeMake(titleNum * ZDScreenSize.width, 0);
    
    //取消水平滚动条
    self.bottomScrollView.showsHorizontalScrollIndicator = NO;
    self.bottomScrollView.showsVerticalScrollIndicator = NO;
    
    //设置分页
    self.bottomScrollView.pagingEnabled = YES;
}


#pragma mark - scroll line方法
- (void)scrollLineToLabel:(ZDLabel *)label
{
    [UIView animateWithDuration:0.3 animations:^{
        self.line.bounds = CGRectMake(0, 0, label.bounds.size.width-ZDPadding, ZDLineHeight);
        self.line.center = CGPointMake(label.center.x, ZDTopScrollHeight - ZDLineHeight * 0.5);
    } completion:^(BOOL finished){
        
//        ZDLog(@"line: %@", NSStringFromCGRect(self.line.frame));
    }];
}


#pragma mark - 实现UIScrollViewDelegate代理方法

//滚动动画停止的时候调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger idx = self.bottomScrollView.contentOffset.x / self.bottomScrollView.frame.size.width;
    
    ZDLabel *label = self.topScrollView.subviews[idx];
    
    CGFloat labelCenterX = label.center.x;
    CGFloat halfScrollViewWidth = self.bottomScrollView.frame.size.width * 0.5;
    
    //计算label中心点，与scrollView中点的偏移
    CGFloat subOffset = labelCenterX - halfScrollViewWidth;
    
    //计算topScrollView的最大偏移
    CGFloat maxOffset = self.topScrollView.contentSize.width - self.topScrollView.frame.size.width;

    
    if (subOffset  < 0)
    {
        subOffset = 0;
    }
    else if (subOffset > maxOffset)
    {
        subOffset = maxOffset;
    }
    
    [self.topScrollView setContentOffset:CGPointMake(subOffset, 0) animated:YES];
    
    [self scrollLineToLabel:label];
}

//减速完毕调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

//滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //计算索引
    CGFloat index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    //当前显示的索引
    NSInteger currentIndex = (NSInteger)index;
    
    //下一个要显示的索引
    NSInteger nextIndex = currentIndex + 1;
    
    //当前显示的标题label
    ZDLabel *currentLabel = self.topScrollView.subviews[currentIndex];
    ZDLabel *nextLabel = nil;
    
    //下一个要显示的标题label
    if (nextIndex < self.centerArr.count)
    {
        ZDBaseViewController *vc = (ZDBaseViewController *)self.childViewControllers[nextIndex];
        
        //当用户滚动的时候，才将控制器的view添加到scrollView中
        if (vc.view.superview == nil)
        {
            vc.view.frame = CGRectMake(nextIndex * scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height);
            
            [scrollView addSubview:vc.view];
        }
        
        nextLabel = self.topScrollView.subviews[nextIndex];
        //下一个即将要展示的label放大
        nextLabel.scale = index - currentIndex;
    }
    
    //定义缩放系数 ———— 当前label缩小
    currentLabel.scale = 1- nextLabel.scale;
}


#pragma mark 懒加载
- (NSMutableArray *)centerArr
{
    if (!_centerArr)
    {
        _centerArr = [NSMutableArray array];
    }
    
    return _centerArr;
}

- (NSMutableArray *)widthArr
{
    if (!_widthArr)
    {
        _widthArr = [NSMutableArray array];
    }
    
    return _widthArr;
}

#pragma mark 懒加载
- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc] init];
        
        _line.backgroundColor = [UIColor redColor];
    }
    
    return _line;
}

@end
